package com.tgz.controller;


import com.alibaba.fastjson.JSON;
import com.tgz.common.BroadCastVo;
import com.tgz.common.R;
import com.tgz.config.MyVar;
import com.tgz.websocket.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 弹幕 前端控制器
 * </p>
 *
 * @author hsj
 * @since 2022-12-22
 */
@RestController
@Slf4j
public class BscreenBroadCastController {

    @Autowired
    public SimpMessagingTemplate template;
    @Autowired
    private MyVar myVar;


    /**
     * 广播
     */
    @MessageMapping("/websocketClientDirectorPushToBroadCast")//stompClient直接发送消息，不走http中转
    public Object pushToOne(@RequestBody BroadCastVo broadCast) {
        String destination = String.format("%s/%s",myVar.getTopic(),broadCast.getRoomId());
        log.info("广播:{}", JSON.toJSONString(broadCast));
        log.info("destination:{}", destination);
        template.convertAndSend(destination, broadCast);
        return R.responseWithDefault();
    }


}
